package es.udc.fic.hamNeggs.web;

import org.apache.log4j.Logger;

import es.udc.fic.hamNeggs.dao.InputSprint;
import es.udc.fic.hamNeggs.manager.ProjectManager;
import es.udc.fic.hamNeggs.manager.SprintManager;
import es.udc.fic.hamNeggs.manager.UserStoriesCommentManager;
import es.udc.fic.hamNeggs.manager.UserStoriesManager;
import es.udc.fic.hamNeggs.pojo.Sprint;
import es.udc.fic.hamNeggs.pojo.UserStories;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;

/**
 * Servlet implementation class SprintManagement
 */
public class SprintManagement extends HttpServlet {
    private static final long   serialVersionUID = 1L;
    private final static Logger LOG              = Logger.getLogger(SprintManagement.class.getName());

    /**
     * @see HttpServlet#HttpServlet()
     */
    public SprintManagement() {
        super();
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    @Override
    protected void doGet(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {
        final HttpSession session = request.getSession(true);

        final Long myProject = (Long) session.getAttribute("project");

        request.setAttribute("rol", session.getAttribute("rol"));

        final ProjectManager prjmng = new ProjectManager();

        final ArrayList<InputSprint> sprints = new ArrayList<InputSprint>();

        for (final Sprint sprint : prjmng.getProject(myProject).getSprints()) {
            final InputSprint iSprint = new InputSprint();
            iSprint.setSprint(sprint);
            sprints.add(iSprint);
        }

        request.setAttribute("rol", session.getAttribute("rol"));
        request.setAttribute("sprints", sprints);

        prjmng.close();

        request.getRequestDispatcher("/sprints.jsp").forward(request, response);

    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    @Override
    protected void doPost(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {
        final SprintManager sprmng = new SprintManager();
        final UserStoriesManager ustmng = new UserStoriesManager();
        final UserStoriesCommentManager cmtmng = new UserStoriesCommentManager();
        final ProjectManager prjmng = new ProjectManager();

        try {
            switch (request.getParameter("action")) {
                case "Delete":
                    final Sprint oldSprint = sprmng.getSprint(new Long(request.getParameter("sprid")));
                    for (final UserStories ustu : oldSprint.getProposedTasks()) {
                        ustu.setSprint(null);
                        ustmng.updateUserStories(ustu);
                    }
                    sprmng.deleteSprint(oldSprint);
                    break;
                default:
                    break;

            }
        } catch (Exception e) {
            String error = "[";
            Enumeration<String> names = request.getParameterNames();
            while (names.hasMoreElements()) {
                final String paramName = names.nextElement();
                final String paramValue = request.getParameter(paramName);
                error = error.concat(paramName + ":" + paramValue + ";");
            }
            error = error.concat("]");
            LOG.error("Error while executing action: " + request.getParameter("action")
                    + "with params: " + error + "\n"
                    , e);
        }

        ustmng.close();
        sprmng.close();
        cmtmng.close();
        prjmng.close();

        doGet(request, response);
    }

}
